package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.SkinCategoryModel;
import com.zw.yzk.kabu.model.SkinModel;
import com.zw.yzk.kabu.view.activity.SkinListActivity;
import com.zw.yzk.kabu.view.activity.SkinPackageActivity;

import java.util.List;

/**
 * Created by skydar on 2017/4/13.
 */

public class SkinCategoryContentListAdapter extends DelegateAdapter.Adapter<SkinCategoryContentListAdapter.Holder> {

    private Context context;
    private LayoutHelper layoutHelper;
    private List<SkinModel> dataList;

    private String subject;
    private int categoryId;
    private int total;

    public SkinCategoryContentListAdapter(Context context, SkinCategoryModel skinCategory) {
        this(context, skinCategory.getSkinModel(), skinCategory.getCategoryId(), skinCategory.getSubject(), skinCategory.getTotal());
    }

    public SkinCategoryContentListAdapter(Context context, List<SkinModel> dataList, int categoryId,  String subject, int total) {
        GridLayoutHelper helper = new GridLayoutHelper(6);
        helper.setBgColor(Color.WHITE);
        int margin = (int) (6 * context.getResources().getDisplayMetrics().density);
        helper.setMarginBottom(margin);
        helper.setPaddingLeft(margin);
        helper.setPaddingRight(margin);
        helper.setAutoExpand(false);
        helper.setSpanCount(3);
        this.layoutHelper = helper;
        this.context = context;
        this.dataList = dataList;

        this.categoryId = categoryId;
        this.subject = subject;
        this.total = total;
    }

    @Override
    public SkinCategoryContentListAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_skin_category_content_list, parent, false);
        return new SkinCategoryContentListAdapter.Holder(view);
    }

    @Override
    public void onBindViewHolder(final SkinCategoryContentListAdapter.Holder holder, final int position) {
        final SkinModel model = dataList.get(position);
        holder.name.setText(model.getName());

        if(position == 5 && total > 6){
            holder.cover.setText("全部("+ total + ")");
            holder.cover.setVisibility(View.VISIBLE);
            holder.name.setVisibility(View.INVISIBLE);

            holder.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, SkinListActivity.class);
                    intent.putExtra(ActivityConstant.SKIN_CATEGORY_ID, categoryId);
                    intent.putExtra(ActivityConstant.SKIN_CATEGORY_SUBJECT, subject);
                    context.startActivity(intent);
                }
            });
        }else{
            holder.cover.setVisibility(View.INVISIBLE);
            holder.name.setVisibility(View.VISIBLE);

            holder.head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, SkinPackageActivity.class);
                    intent.putExtra(ActivityConstant.SKIN_PACKAGE_ID, model.getId());
                    intent.putExtra(ActivityConstant.SKIN_PACKAGE_NAME, model.getName());
                    context.startActivity(intent);
                }
            });
        }

        Glide.with(context)
                .load(model.getCoverPath())
                .error(R.drawable.im_default)
                .placeholder(R.drawable.im_default)
                .centerCrop()
                .into(holder.head);
    }

    @Override
    public int getItemCount() {
        return dataList.size() > 6 ? 6 : dataList.size();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    public static class Holder extends RecyclerView.ViewHolder {

        @Bind(R.id.head)
        ImageView head;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.tv_cover)
        TextView cover;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}